package handler

import (
	"github.com/gin-gonic/gin"
	request2 "zg3yk/api/request"
	"zg3yk/interface/dao"
	"zg3yk/interface/models"
	"zg3yk/interface/queue"
	"zg3yk/pkg"
)

func ChatAdd(context *gin.Context) {

	var req request2.RoomAdd
	err := context.ShouldBind(&req)
	if err != nil {
		context.JSON(500, gin.H{
			"code": 500,
			"msg":  err.Error(),
			"data": nil,
		})
		return

	}

	if req.Id == 0 {
		context.JSON(500, gin.H{
			"code": 500,
			"msg":  "账号不存在",
			"data": nil,
		})
		return

	}

	bdy := pkg.Bdy(req.Content)

	if bdy == false {
		context.JSON(200, gin.H{
			"code": 200,
			"msg":  "污秽吃鱼",
			"data": bdy,
		})
		return
	}

	chat := models.Rooms{
		Room:    req.Room,
		Content: req.Content,
		Status:  req.Status,
		UserId:  context.GetUint("userId"),
	}

	dao.Create(&chat, &chat)
	context.JSON(200, gin.H{
		"code": 200,
		"msg":  "创建成功",
		"data": chat,
	})

}

func ChatFind(context *gin.Context) {
	var req request2.RoomFind
	err := context.ShouldBind(&req)
	if err != nil {
		context.JSON(500, gin.H{
			"code": 500,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}

	var room models.Rooms
	dao.FindWhere(&room, &room)

	queue.PublishPub("notification", room)

	context.JSON(200, gin.H{
		"code": 200,
		"msg":  "聊天",
		"data": room,
	})

}
